#!/bin/bash

SUDO="sudo "
if [[ $(id -u) -eq 0 ]]; then SUDO=""; fi

read -p "Enter openjdk version (default 17, e.g. 8 | 9 | 11 | 17 | 18 | 19 | 20 | 21): " version </dev/tty
version=$(echo "$version" | tr -s '[:space:]')
if [[ "$version" == "" ]]; then version="17.0.2"; fi

$SUDO mkdir -p /opt/openjdk/${version}

if [[ -x /opt/openjdk/$version/bin/java ]]; then
    read -p "openjdk[${version}] is installed, reinstall it? (y/N) " reinstall </dev/tty
    reinstall=$(echo "$reinstall" | tr -s '[:space:]')
    if [[ "$reinstall" != [Yy] ]]; then exit 0; fi
fi
$SUDO rm -rf /opt/openjdk/${version}/* openjdk-${version}_linux-x64_bin.tar.gz

case $version in
    8)
        wget https://mirrors.huaweicloud.com/openjdk/java-jse-ri/jdk8u40/openjdk-8u40-b25-linux-x64-10_feb_2015.tar.gz -O openjdk-${version}_linux-x64_bin.tar.gz
        ;;
    9)
        wget https://mirrors.huaweicloud.com/openjdk/9.0.4/openjdk-9.0.4_linux-x64_bin.tar.gz -O openjdk-${version}_linux-x64_bin.tar.gz
        ;;
    11)
        wget https://mirrors.huaweicloud.com/openjdk/11.0.2/openjdk-11.0.2_linux-x64_bin.tar.gz -O openjdk-${version}_linux-x64_bin.tar.gz
        ;;
    17)
        wget https://mirrors.huaweicloud.com/openjdk/17.0.2/openjdk-17.0.2_linux-x64_bin.tar.gz -O openjdk-${version}_linux-x64_bin.tar.gz
        ;;
    18)
        wget https://mirrors.huaweicloud.com/openjdk/18.0.2/openjdk-18.0.2_linux-x64_bin.tar.gz -O openjdk-${version}_linux-x64_bin.tar.gz
        ;;
    19)
        wget https://mirrors.huaweicloud.com/openjdk/19.0.2/openjdk-19.0.2_linux-x64_bin.tar.gz -O openjdk-${version}_linux-x64_bin.tar.gz
        ;;
    20)
        wget https://mirrors.huaweicloud.com/openjdk/20.0.2/openjdk-20.0.2_linux-x64_bin.tar.gz -O openjdk-${version}_linux-x64_bin.tar.gz
        ;;
    21)
        wget https://mirrors.huaweicloud.com/openjdk/21.0.2/openjdk-21.0.2_linux-x64_bin.tar.gz -O openjdk-${version}_linux-x64_bin.tar.gz
        ;;
    *)
        echo "error version"
        exit -1
esac
if [[ ! -f openjdk-${version}_linux-x64_bin.tar.gz ]]; then echo "download failed."; exit -1; fi

$SUDO tar -zxvf openjdk-${version}_linux-x64_bin.tar.gz -C /opt/openjdk/${version} --strip-components=1
rm -f openjdk-${version}_linux-x64_bin.tar.gz

mkdir -p $HOME/.m2

cat << "EOF" | tee $HOME/.m2/settings.xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <mirrors>
        <mirror>
            <id>nexus-tencentyun</id>
            <mirrorOf>central</mirrorOf>
            <name>Nexus tencentyun</name>
            <url>http://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>
        </mirror>
    </mirrors>
    <!-- <proxies>
        <proxy>
            <id>optional</id>
            <active>true</active>
            <protocol>http</protocol>
            <host>127.0.0.1</host>
            <port>7890</port>
        </proxy>
    </proxies> -->
</settings>
EOF

echo -e "\nMake sure that the following content exists in the \$HOME/.bashrc"
echo -e "\texport JAVA_HOME=/opt/openjdk/${version}"
echo -e "\texport PATH=\$JAVA_HOME/bin:\$PATH"
